<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>7.1 认识异步 | 引言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c07/s02.html" />
    
    
    <link rel="prev" href="../c07/index.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="7.1"
        data-chapter-title="7.1 认识异步"
        data-filepath="c07/s01.md"
        data-basepath=".."
        data-revision="Fri Feb 10 2017 17:36:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        1 关于Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        1.1 Tornado是为何物
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        1.2 Tornado与Django
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        2 初识Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        2.1 安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        2.2 Hello Itcast
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c02/s03.html">
            
                
                    <a href="../c02/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        2.3 httpserver
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="c02/s04.html">
            
                
                    <a href="../c02/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        2.4 options
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c02/s05.html">
            
                
                    <a href="../c02/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        2.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        3 深入Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        3.1 Application
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        3.2 输入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        3.3 输出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        3.4 接口与调用顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        3.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        4 模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        4.1 静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        4.2 使用模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        4.3 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        5 数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        5.1 数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        5.2 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="c06/index.html">
            
                
                    <a href="../c06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        6 安全应用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="c06/s01.html">
            
                
                    <a href="../c06/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        6.1 Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="c06/s02.html">
            
                
                    <a href="../c06/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        6.2 XSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="c06/s03.html">
            
                
                    <a href="../c06/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        6.3 用户验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="c06/s04.html">
            
                
                    <a href="../c06/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        6.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="c07/index.html">
            
                
                    <a href="../c07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        7 异步与WebSocket
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="7.1" data-path="c07/s01.html">
            
                
                    <a href="../c07/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        7.1 认识异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="c07/s02.html">
            
                
                    <a href="../c07/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        7.2 Tornado异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="c07/s03.html">
            
                
                    <a href="../c07/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        7.3 WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="c07/s04.html">
            
                
                    <a href="../c07/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        7.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="c08/index.html">
            
                
                    <a href="../c08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        8 部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="c08/s01.html">
            
                
                    <a href="../c08/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        8.1 部署Tornado
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >引言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="71-&#x8BA4;&#x8BC6;&#x5F02;&#x6B65;">7.1 &#x8BA4;&#x8BC6;&#x5F02;&#x6B65;</h1>
<h2 id="1-&#x540C;&#x6B65;">1. &#x540C;&#x6B65;</h2>
<p>&#x6211;&#x4EEC;&#x7528;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x6A21;&#x62DF;&#x4E24;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#xFF0C;&#x5E76;&#x4F9D;&#x6B21;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_a</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x6A21;&#x62DF;&#x8BF7;&#x6C42;a&quot;&quot;&quot;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&apos;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&apos;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_b</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x6A21;&#x62DF;&#x8BF7;&#x6C42;b&quot;&quot;&quot;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&apos;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&apos;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&apos;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x6A21;&#x62DF;tornado&#x6846;&#x67B6;&#xFF0C;&#x5904;&#x7406;&#x4E24;&#x4E2A;&#x8BF7;&#x6C42;&quot;&quot;&quot;</span>
    req_a()
    req_b()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    main()
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
</code></pre>
<p><strong>&#x540C;&#x6B65;&#x662F;&#x6309;&#x90E8;&#x5C31;&#x73ED;&#x7684;&#x4F9D;&#x6B21;&#x6267;&#x884C;&#xFF0C;&#x59CB;&#x7EC8;&#x6309;&#x7167;&#x540C;&#x4E00;&#x4E2A;&#x6B65;&#x8C03;&#x6267;&#x884C;&#xFF0C;&#x4E0A;&#x4E00;&#x4E2A;&#x6B65;&#x9AA4;&#x672A;&#x6267;&#x884C;&#x5B8C;&#x4E0D;&#x4F1A;&#x6267;&#x884C;&#x4E0B;&#x4E00;&#x6B65;&#x3002;</strong></p>
<p><strong>&#x60F3;&#x4E00;&#x60F3;</strong>&#xFF0C;&#x5982;&#x679C;&#x5728;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&#x65F6;&#x9700;&#x8981;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x8017;&#x65F6;&#x7684;&#x5DE5;&#x4F5C;&#xFF08;&#x5982;IO&#xFF09;&#xFF0C;&#x5176;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x5982;&#x4F55;&#xFF1F;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> time

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">long_io</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x6A21;&#x62DF;&#x8017;&#x65F6;IO&#x64CD;&#x4F5C;&quot;&quot;&quot;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;&quot;</span>
    time.sleep(<span class="hljs-number">5</span>)
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&quot;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;io result&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_a</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>
    ret = long_io()
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;ret: %s&quot;</span> % ret
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_b</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
    req_a()
    req_b()

<span class="hljs-keyword">if</span> __name__==<span class="hljs-string">&quot;__main__&quot;</span>:
    main()
</code></pre>
<p>&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-python">&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;
&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x6D4B;&#x8BD5;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x770B;&#x5230;&#x8017;&#x65F6;&#x7684;&#x64CD;&#x4F5C;&#x4F1A;&#x5C06;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x963B;&#x585E;&#x4F4F;&#xFF0C;&#x5373;req_a&#x672A;&#x5904;&#x7406;&#x5B8C;req_b&#x662F;&#x65E0;&#x6CD5;&#x6267;&#x884C;&#x7684;&#x3002;</p>
<h4 id="&#x6211;&#x4EEC;&#x600E;&#x4E48;&#x89E3;&#x51B3;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#x963B;&#x585E;&#x4EE3;&#x7801;&#x6267;&#x884C;&#xFF1F;">&#x6211;&#x4EEC;&#x600E;&#x4E48;&#x89E3;&#x51B3;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#x963B;&#x585E;&#x4EE3;&#x7801;&#x6267;&#x884C;&#xFF1F;</h4>
<h2 id="2-&#x5F02;&#x6B65;">2. &#x5F02;&#x6B65;</h2>
<p>&#x5BF9;&#x4E8E;&#x8017;&#x65F6;&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5176;&#x4EA4;&#x7ED9;&#x522B;&#x4EBA;&#xFF08;&#x5982;&#x5176;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF09;&#x53BB;&#x6267;&#x884C;&#xFF0C;&#x800C;&#x6211;&#x4EEC;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x5904;&#x7406;&#xFF0C;&#x5F53;&#x522B;&#x4EBA;&#x6267;&#x884C;&#x5B8C;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#x540E;&#x518D;&#x5C06;&#x7ED3;&#x679C;&#x53CD;&#x9988;&#x7ED9;&#x6211;&#x4EEC;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x6240;&#x8BF4;&#x7684;&#x5F02;&#x6B65;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x7528;&#x5BB9;&#x6613;&#x7406;&#x89E3;&#x7684;&#x7EBF;&#x7A0B;&#x673A;&#x5236;&#x6765;&#x5B9E;&#x73B0;&#x5F02;&#x6B65;&#x3002;</p>
<h3 id="21-&#x56DE;&#x8C03;&#x5199;&#x6CD5;&#x5B9E;&#x73B0;&#x539F;&#x7406;">2.1 &#x56DE;&#x8C03;&#x5199;&#x6CD5;&#x5B9E;&#x73B0;&#x539F;&#x7406;</h3>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> thread

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">long_io</span><span class="hljs-params">(callback)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x5C06;&#x8017;&#x65F6;&#x7684;&#x64CD;&#x4F5C;&#x4EA4;&#x7ED9;&#x53E6;&#x4E00;&#x7EBF;&#x7A0B;&#x6765;&#x5904;&#x7406;&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun</span><span class="hljs-params">(cb)</span>:</span> <span class="hljs-comment"># &#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&quot;&quot;&quot;</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;&quot;</span>
        time.sleep(<span class="hljs-number">5</span>)
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;&quot;</span>
        cb(<span class="hljs-string">&quot;io result&quot;</span>)  <span class="hljs-comment"># &#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;</span>
    thread.start_new_thread(fun, (callback,))  <span class="hljs-comment"># &#x5F00;&#x542F;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x8017;&#x65F6;&#x64CD;&#x4F5C;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_finish</span><span class="hljs-params">(ret)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56DE;&#x8C03;&#x51FD;&#x6570;&quot;&quot;&quot;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;on_finish&quot;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;ret: %s&quot;</span> % ret
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;on_finish&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_a</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span> 
    long_io(on_finish)
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x79BB;&#x5F00;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_b</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>
    time.sleep(<span class="hljs-number">2</span>) <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x6B64;&#x53E5;&#x6765;&#x7A81;&#x51FA;&#x663E;&#x793A;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x7684;&#x8FC7;&#x7A0B;</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
    req_a()
    req_b()
    <span class="hljs-keyword">while</span> <span class="hljs-number">1</span>: <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x6B64;&#x53E5;&#x9632;&#x6B62;&#x7A0B;&#x5E8F;&#x9000;&#x51FA;&#xFF0C;&#x4FDD;&#x8BC1;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x5B8C;</span>
        <span class="hljs-keyword">pass</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    main()
</code></pre>
<p>&#x6267;&#x884C;&#x8FC7;&#x7A0B;:</p>
<pre><code class="lang-python">&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x79BB;&#x5F00;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;
&#x5F00;&#x59CB;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;on_finish
ret: io result
&#x5B8C;&#x6210;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x51FD;&#x6570;on_finish
</code></pre>
<p><strong>&#x5F02;&#x6B65;&#x7684;&#x7279;&#x70B9;&#x662F;&#x7A0B;&#x5E8F;&#x5B58;&#x5728;&#x591A;&#x4E2A;&#x6B65;&#x8C03;&#xFF0C;&#x5373;&#x672C;&#x5C5E;&#x4E8E;&#x540C;&#x4E00;&#x4E2A;&#x8FC7;&#x7A0B;&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x80FD;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x6B65;&#x8C03;&#x4E0A;&#x540C;&#x65F6;&#x6267;&#x884C;&#x3002;</strong></p>
<h3 id="22-&#x534F;&#x7A0B;&#x5199;&#x6CD5;&#x5B9E;&#x73B0;&#x539F;&#x7406;">2.2 &#x534F;&#x7A0B;&#x5199;&#x6CD5;&#x5B9E;&#x73B0;&#x539F;&#x7406;</h3>
<p>&#x5728;&#x4F7F;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5199;&#x5F02;&#x6B65;&#x7A0B;&#x5E8F;&#x65F6;&#xFF0C;&#x9700;&#x5C06;&#x672C;&#x5C5E;&#x4E8E;&#x4E00;&#x4E2A;&#x6267;&#x884C;&#x903B;&#x8F91;&#xFF08;&#x5904;&#x7406;&#x8BF7;&#x6C42;a&#xFF09;&#x7684;&#x4EE3;&#x7801;&#x62C6;&#x5206;&#x6210;&#x4E24;&#x4E2A;&#x51FD;&#x6570;req_a&#x548C;on_finish&#xFF0C;&#x8FD9;&#x4E0E;&#x540C;&#x6B65;&#x7A0B;&#x5E8F;&#x7684;&#x5199;&#x6CD5;&#x76F8;&#x5DEE;&#x5F88;&#x5927;&#x3002;&#x800C;&#x540C;&#x6B65;&#x7A0B;&#x5E8F;&#x66F4;&#x4FBF;&#x4E8E;&#x7406;&#x89E3;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x80FD;&#x5426;&#x7528;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x7684;&#x5199;&#x6CD5;&#x6765;&#x7F16;&#x5199;&#x5F02;&#x6B65;&#x7A0B;&#x5E8F;&#xFF1F;</p>
<h4 id="&#x56DE;&#x60F3;yield&#x5173;&#x952E;&#x5B57;&#x7684;&#x4F5C;&#x7528;&#xFF1F;">&#x56DE;&#x60F3;yield&#x5173;&#x952E;&#x5B57;&#x7684;&#x4F5C;&#x7528;&#xFF1F;</h4>
<h4 id="&#x521D;&#x59CB;&#x7248;&#x672C;">&#x521D;&#x59CB;&#x7248;&#x672C;</h4>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> thread

gen = <span class="hljs-keyword">None</span> <span class="hljs-comment"># &#x5168;&#x5C40;&#x751F;&#x6210;&#x5668;&#xFF0C;&#x4F9B;long_io&#x4F7F;&#x7528;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">long_io</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun</span><span class="hljs-params">()</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;&quot;</span>
        <span class="hljs-keyword">global</span> gen
        time.sleep(<span class="hljs-number">5</span>)
        <span class="hljs-keyword">try</span>:
            <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;send&#x7ED3;&#x679C;&#x5524;&#x9192;&#x6302;&#x8D77;&#x7A0B;&#x5E8F;&#x7EE7;&#x7EED;&#x6267;&#x884C;&quot;</span>
            gen.send(<span class="hljs-string">&quot;io result&quot;</span>)  <span class="hljs-comment"># &#x4F7F;&#x7528;send&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x5E76;&#x5524;&#x9192;&#x7A0B;&#x5E8F;&#x7EE7;&#x7EED;&#x6267;&#x884C;</span>
        <span class="hljs-keyword">except</span> StopIteration: <span class="hljs-comment"># &#x6355;&#x83B7;&#x751F;&#x6210;&#x5668;&#x5B8C;&#x6210;&#x8FED;&#x4EE3;&#xFF0C;&#x9632;&#x6B62;&#x7A0B;&#x5E8F;&#x9000;&#x51FA;</span>
            <span class="hljs-keyword">pass</span>
    thread.start_new_thread(fun, ())

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_a</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>
    ret = <span class="hljs-keyword">yield</span> long_io()
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;ret: %s&quot;</span> % ret
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_b</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>
    time.sleep(<span class="hljs-number">2</span>)
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">global</span> gen
    gen = req_a()
    gen.next() <span class="hljs-comment"># &#x5F00;&#x542F;&#x751F;&#x6210;&#x5668;req_a&#x7684;&#x6267;&#x884C;</span>
    req_b()
    <span class="hljs-keyword">while</span> <span class="hljs-number">1</span>:
        <span class="hljs-keyword">pass</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    main()
</code></pre>
<p>&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-python">&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;send&#x7ED3;&#x679C;&#x5524;&#x9192;&#x6302;&#x8D77;&#x7A0B;&#x5E8F;&#x7EE7;&#x7EED;&#x6267;&#x884C;
ret: io result
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
</code></pre>
<h4 id="&#x5347;&#x7EA7;&#x7248;&#x672C;">&#x5347;&#x7EA7;&#x7248;&#x672C;</h4>
<p>&#x6211;&#x4EEC;&#x5728;&#x4E0A;&#x9762;&#x7F16;&#x5199;&#x51FA;&#x7684;&#x7248;&#x672C;&#x867D;&#x7136;req_a&#x7684;&#x7F16;&#x5199;&#x65B9;&#x5F0F;&#x5F88;&#x7C7B;&#x4F3C;&#x4E0E;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#xFF0C;&#x4F46;&#x662F;&#x5728;main&#x4E2D;&#x8C03;&#x7528;req_a&#x7684;&#x65F6;&#x5019;&#x5374;&#x4E0D;&#x80FD;&#x5C06;&#x5176;&#x7B80;&#x5355;&#x7684;&#x89C6;&#x4E3A;&#x666E;&#x901A;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x662F;&#x9700;&#x8981;&#x4F5C;&#x4E3A;&#x751F;&#x6210;&#x5668;&#x5BF9;&#x5F85;&#x3002;</p>
<p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x8BD5;&#x56FE;&#x5C1D;&#x8BD5;&#x4FEE;&#x6539;&#xFF0C;&#x8BA9;req_a&#x4E0E;main&#x7684;&#x7F16;&#x5199;&#x90FD;&#x7C7B;&#x4F3C;&#x4E0E;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> thread

gen = <span class="hljs-keyword">None</span> <span class="hljs-comment"># &#x5168;&#x5C40;&#x751F;&#x6210;&#x5668;&#xFF0C;&#x4F9B;long_io&#x4F7F;&#x7528;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">gen_coroutine</span><span class="hljs-params">(f)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span><span class="hljs-params">(*args, **kwargs)</span>:</span>
        <span class="hljs-keyword">global</span> gen
        gen = f()
        gen.next()
    <span class="hljs-keyword">return</span> wrapper

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">long_io</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun</span><span class="hljs-params">()</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;&quot;</span>
        <span class="hljs-keyword">global</span> gen
        time.sleep(<span class="hljs-number">5</span>)
        <span class="hljs-keyword">try</span>:
            <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;send&#x7ED3;&#x679C;&#x5524;&#x9192;&#x6302;&#x8D77;&#x7A0B;&#x5E8F;&#x7EE7;&#x7EED;&#x6267;&#x884C;&quot;</span>
            gen.send(<span class="hljs-string">&quot;io result&quot;</span>)  <span class="hljs-comment"># &#x4F7F;&#x7528;send&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x5E76;&#x5524;&#x9192;&#x7A0B;&#x5E8F;&#x7EE7;&#x7EED;&#x6267;&#x884C;</span>
        <span class="hljs-keyword">except</span> StopIteration: <span class="hljs-comment"># &#x6355;&#x83B7;&#x751F;&#x6210;&#x5668;&#x5B8C;&#x6210;&#x8FED;&#x4EE3;&#xFF0C;&#x9632;&#x6B62;&#x7A0B;&#x5E8F;&#x9000;&#x51FA;</span>
            <span class="hljs-keyword">pass</span>
    thread.start_new_thread(fun, ())

<span class="hljs-decorator">@gen_coroutine</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_a</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>
    ret = <span class="hljs-keyword">yield</span> long_io()
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;ret: %s&quot;</span> % ret
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_b</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>
    time.sleep(<span class="hljs-number">2</span>)
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
    req_a()
    req_b()
    <span class="hljs-keyword">while</span> <span class="hljs-number">1</span>:
        <span class="hljs-keyword">pass</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    main()
</code></pre>
<p>&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-python">&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;send&#x7ED3;&#x679C;&#x5524;&#x9192;&#x6302;&#x8D77;&#x7A0B;&#x5E8F;&#x7EE7;&#x7EED;&#x6267;&#x884C;
ret: io result
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
</code></pre>
<h4 id="&#x6700;&#x7EC8;&#x7248;&#x672C;">&#x6700;&#x7EC8;&#x7248;&#x672C;</h4>
<p>&#x521A;&#x521A;&#x5B8C;&#x6210;&#x7684;&#x7248;&#x672C;&#x4F9D;&#x7136;&#x4E0D;&#x7406;&#x60F3;&#xFF0C;&#x56E0;&#x4E3A;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x5168;&#x5C40;&#x53D8;&#x91CF;gen&#x6765;&#x4F9B;long_io&#x4F7F;&#x7528;&#x3002;&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x518D;&#x6B21;&#x6539;&#x5199;&#x7A0B;&#x5E8F;&#xFF0C;&#x6D88;&#x9664;&#x5168;&#x5C40;&#x53D8;&#x91CF;gen&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> thread

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">gen_coroutine</span><span class="hljs-params">(f)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span><span class="hljs-params">(*args, **kwargs)</span>:</span>
        gen_f = f()  <span class="hljs-comment"># gen_f&#x4E3A;&#x751F;&#x6210;&#x5668;req_a</span>
        r = gen_f.next()  <span class="hljs-comment"># r&#x4E3A;&#x751F;&#x6210;&#x5668;long_io</span>
        <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fun</span><span class="hljs-params">(g)</span>:</span>
            ret = g.next() <span class="hljs-comment"># &#x6267;&#x884C;&#x751F;&#x6210;&#x5668;long_io</span>
            <span class="hljs-keyword">try</span>:
                gen_f.send(ret) <span class="hljs-comment"># &#x5C06;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x7ED9;req_a&#x5E76;&#x4F7F;&#x5176;&#x7EE7;&#x7EED;&#x6267;&#x884C;</span>
            <span class="hljs-keyword">except</span> StopIteration:
                <span class="hljs-keyword">pass</span>
        thread.start_new_thread(fun, (r,))
    <span class="hljs-keyword">return</span> wrapper

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">long_io</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;&quot;</span>
    time.sleep(<span class="hljs-number">5</span>)
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;yield&#x56DE;&#x64CD;&#x4F5C;&#x7ED3;&#x679C;&quot;</span>
    <span class="hljs-keyword">yield</span> <span class="hljs-string">&quot;io result&quot;</span>

<span class="hljs-decorator">@gen_coroutine</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_a</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>
    ret = <span class="hljs-keyword">yield</span> long_io()
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;ret: %s&quot;</span> % ret
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req_b</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>
    time.sleep(<span class="hljs-number">2</span>)
    <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b&quot;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
    req_a()
    req_b()
    <span class="hljs-keyword">while</span> <span class="hljs-number">1</span>:
        <span class="hljs-keyword">pass</span>

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    main()
</code></pre>
<p>&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-python">&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
&#x5F00;&#x59CB;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5F00;&#x59CB;&#x6267;&#x884C;IO&#x64CD;&#x4F5C;
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_b
&#x5B8C;&#x6210;IO&#x64CD;&#x4F5C;&#xFF0C;<span class="hljs-keyword">yield</span>&#x56DE;&#x64CD;&#x4F5C;&#x7ED3;&#x679C;
ret: io result
&#x5B8C;&#x6210;&#x5904;&#x7406;&#x8BF7;&#x6C42;req_a
</code></pre>
<p><strong>&#x8FD9;&#x4E2A;&#x6700;&#x7EC8;&#x7248;&#x672C;&#x5C31;&#x662F;&#x7406;&#x89E3;Tornado&#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#x539F;&#x7406;&#x7684;&#x6700;&#x7B80;&#x6613;&#x6A21;&#x578B;&#xFF0C;&#x4F46;&#x662F;&#xFF0C;Tornado&#x5B9E;&#x73B0;&#x5F02;&#x6B65;&#x7684;&#x673A;&#x5236;&#x4E0D;&#x662F;&#x7EBF;&#x7A0B;&#xFF0C;&#x800C;&#x662F;epoll&#xFF0C;&#x5373;&#x5C06;&#x5F02;&#x6B65;&#x8FC7;&#x7A0B;&#x4EA4;&#x7ED9;epoll&#x6267;&#x884C;&#x5E76;&#x8FDB;&#x884C;&#x76D1;&#x89C6;&#x56DE;&#x8C03;&#x3002;</strong></p>
<p><strong>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x4E00;&#x70B9;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x7684;&#x7248;&#x672C;&#x4E25;&#x683C;&#x610F;&#x4E49;&#x4E0A;&#x6765;&#x8BF4;&#x4E0D;&#x80FD;&#x7B97;&#x662F;&#x534F;&#x7A0B;&#xFF0C;&#x56E0;&#x4E3A;&#x4E24;&#x4E2A;&#x7A0B;&#x5E8F;&#x7684;&#x6302;&#x8D77;&#x4E0E;&#x5524;&#x9192;&#x662F;&#x5728;&#x4E24;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E0A;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x800C;Tornado&#x5229;&#x7528;epoll&#x6765;&#x5B9E;&#x73B0;&#x5F02;&#x6B65;&#xFF0C;&#x7A0B;&#x5E8F;&#x7684;&#x6302;&#x8D77;&#x4E0E;&#x5524;&#x9192;&#x59CB;&#x7EC8;&#x5728;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E0A;&#xFF0C;&#x7531;Tornado&#x81EA;&#x5DF1;&#x6765;&#x8C03;&#x5EA6;&#xFF0C;&#x5C5E;&#x4E8E;&#x771F;&#x6B63;&#x610F;&#x4E49;&#x4E0A;&#x7684;&#x534F;&#x7A0B;&#x3002;&#x867D;&#x5982;&#x6B64;&#xFF0C;&#x5E76;&#x4E0D;&#x59A8;&#x788D;&#x6211;&#x4EEC;&#x7406;&#x89E3;Tornado&#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#x7684;&#x539F;&#x7406;&#x3002;</strong></p>
<h3 id="&#x601D;&#x8003;">&#x601D;&#x8003;</h3>
<ol>
<li>Tornado&#x91CC;&#x7684;&#x5F02;&#x6B65;&#x5C31;&#x662F;&#x534F;&#x7A0B;&#xFF0C;&#x8FD9;&#x53E5;&#x8BDD;&#x5BF9;&#x5417;&#xFF1F;</li>
<li>Tornado&#x4E2D;&#x51FA;&#x73B0;yield&#x5C31;&#x662F;&#x5F02;&#x6B65;&#xFF0C;&#x8FD9;&#x53E5;&#x8BDD;&#x5BF9;&#x5417;&#xFF1F;</li>
<li>&#x600E;&#x4E48;&#x7406;&#x89E3;yield&#x5C06;&#x7A0B;&#x5E8F;&#x6302;&#x8D77;&#xFF1F;&#x5728;Tornado&#x4E2D;&#x53C8;&#x5982;&#x4F55;&#x7406;&#x89E3;yield&#x6302;&#x8D77;&#x7A0B;&#x5E8F;&#x5B9E;&#x73B0;&#x5F02;&#x6B65;&#xFF1F;</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c07/index.html" class="navigation navigation-prev " aria-label="Previous page: 7 异步与WebSocket"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c07/s02.html" class="navigation navigation-next " aria-label="Next page: 7.2 Tornado异步"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
